package flc.ast.adapter;

import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import flc.ast.R;
import me.zhouzhuo.zzratingbar.ZzRatingBar;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class MovieChildAdapter extends StkProviderMultiAdapter<StkResBean> {

    public MovieChildAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new MovieChildProvider());
    }

    private class MovieChildProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_movie_child;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {

            Glide.with(baseViewHolder.itemView).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivMovieChildItemImg));
            baseViewHolder.setText(R.id.tvMovieChildItemName, bean.getName());
            baseViewHolder.setText(R.id.tvMovieChildItemDesc, bean.getDesc());

            String str = new Gson().toJson(bean.getExtra());

            try {
                if (str != null && !str.equals("\"\"")) {
                    JSONObject extra = new JSONObject(str);
                    baseViewHolder.setText(R.id.tvMovieChildItemDate, extra.optString("releaseDate"));
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            String tag = getContext().getString(R.string.type_text);
            for (String s : bean.getTagNameList()) {
                tag += s + " / ";
            }
            baseViewHolder.setText(R.id.tvMovieChildItemType, tag);

            //评分
            double score = (double) bean.getScore_total() / (bean.getScore_count() == 0 ? 1 : bean.getScore_count());
            String formattedNumber = String.format("%.1f", score);
            baseViewHolder.setText(R.id.tvMovieChildItemScore, formattedNumber);

            ZzRatingBar zzRatingBar = baseViewHolder.getView(R.id.zzratingbar);
            if (score > 8) {
                zzRatingBar.setRating(5);
            } else if (score > 6) {
                zzRatingBar.setRating(4);
            } else if (score > 4) {
                zzRatingBar.setRating(3);
            } else if (score > 2) {
                zzRatingBar.setRating(2);
            } else {
                zzRatingBar.setRating(1);
            }
        }
    }

}
